using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using System;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
using Server = Microsoft.SqlServer.Management.Smo.Server;
	internal class ScriptObjectCommandFactory : ScriptCommandFactory
	{
		private  Server server;

		private ScriptActions scriptAction;

		private TableChildObjects childObjects;

		private ScriptTableCommand cachedTable;

		private ScriptUDDTCommand cachedUDDTCommand;

		private ScriptSqlAssemblyCommand cachedSqlAssemblyCommand;

		private ScriptDatabaseTriggerCommand cachedDatabaseTriggerCommand;

		private ScriptApplicationRoleCommand cachedApplicationRoleCommand;

		private ScriptObjectCommand cachedObject;

		private ScriptDatabaseRoleCommand cachedDatabaseRole;

		private ScriptFullTextCatalogCommand cachedFullTextCatalogCommand;

		private ScriptDatabaseCommand cachedDatabaseCommand;

		private ScriptUserCommand cachedUser;

		public ScriptObjectCommandFactory(Server server, ScriptActions scriptAction, TableChildObjects childObjects)
		{
			if (server == null)
			{
				throw new ArgumentNullException("server");
			}
			this.server = server;
			this.scriptAction = scriptAction;
			this.childObjects = childObjects;
		}

		public override ScriptCommand GetScriptCommand(Urn item, ObjectScriptedHandler scriptTarget)
		{
			if (item == null)
			{
				throw new ArgumentNullException("item");
			}
			if (item.ToString().Length == 0)
			{
				throw new ArgumentException("item");
			}
			string type;
			ScriptCommand result;
			switch (type = item.Type)
			{
			case "Database":
				result = this.GetScriptDatabaseCommand(this.server, scriptTarget, this.scriptAction, this.childObjects);
				return result;
			case "Table":
				result = this.GetScriptTable(this.server, scriptTarget, this.scriptAction, this.childObjects);
				return result;
			case "Role":
				result = this.GetScriptRole(item, this.server, scriptTarget, this.scriptAction);
				return result;
			case "User":
				result = this.GetScriptUser(this.server, scriptTarget, this.scriptAction);
				return result;
			case "FullTextCatalog":
				result = this.GetScriptFullTextCatalogCommand(this.server, scriptTarget, this.scriptAction);
				return result;
			case "UserDefinedDataType":
				result = this.GetScriptUDDTCommand(this.server, scriptTarget, this.scriptAction, this.childObjects);
				return result;
			case "SqlAssembly":
				result = this.GetScriptAssemblyCommand(this.server, scriptTarget, this.scriptAction);
				return result;
			case "ApplicationRole":
				result = this.GetScriptApplicationRoleCommand(this.server, scriptTarget, this.scriptAction);
				return result;
			case "DdlTrigger":
				result = this.GetScriptDatabaseTriggerCommand(this.server, scriptTarget, this.scriptAction);
				return result;
			}
			result = this.GetScriptObject(this.server, scriptTarget, this.scriptAction);
			return result;
		}

		private ScriptDatabaseTriggerCommand GetScriptDatabaseTriggerCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptActions)
		{
			if (this.cachedDatabaseTriggerCommand == null)
			{
				this.cachedDatabaseTriggerCommand = new ScriptDatabaseTriggerCommand(server, scriptTarget, this.scriptAction);
			}
			return this.cachedDatabaseTriggerCommand;
		}

		private ScriptApplicationRoleCommand GetScriptApplicationRoleCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptActions)
		{
			if (this.cachedApplicationRoleCommand == null)
			{
				this.cachedApplicationRoleCommand = new ScriptApplicationRoleCommand(server, scriptTarget, this.scriptAction);
			}
			return this.cachedApplicationRoleCommand;
		}

		private ScriptSqlAssemblyCommand GetScriptAssemblyCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptActions)
		{
			if (this.cachedSqlAssemblyCommand == null)
			{
				this.cachedSqlAssemblyCommand = new ScriptSqlAssemblyCommand(server, scriptTarget, this.scriptAction);
			}
			return this.cachedSqlAssemblyCommand;
		}

		private ScriptUDDTCommand GetScriptUDDTCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptActions, TableChildObjects tableChildObjects)
		{
			if (this.cachedUDDTCommand == null)
			{
				this.cachedUDDTCommand = new ScriptUDDTCommand(server, scriptTarget, this.scriptAction, this.childObjects);
			}
			return this.cachedUDDTCommand;
		}

		private ScriptCommand GetScriptDatabaseCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptActions, TableChildObjects tableChildObjects)
		{
			if (this.cachedDatabaseCommand == null)
			{
				this.cachedDatabaseCommand = new ScriptDatabaseCommand(server, scriptTarget, scriptActions);
			}
			return this.cachedDatabaseCommand;
		}

		private ScriptCommand GetScriptFullTextCatalogCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptActions)
		{
			if (this.cachedFullTextCatalogCommand == null)
			{
				this.cachedFullTextCatalogCommand = new ScriptFullTextCatalogCommand(server, scriptTarget, scriptActions);
			}
			return this.cachedFullTextCatalogCommand;
		}

		private ScriptCommand GetScriptTable(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction, TableChildObjects childObjects)
		{
			if (this.cachedTable == null)
			{
				this.cachedTable = new ScriptTableCommand(server, scriptTarget, scriptAction, childObjects);
			}
			return this.cachedTable;
		}

		private ScriptCommand GetScriptObject(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction)
		{
			if (this.cachedObject == null)
			{
				this.cachedObject = new ScriptObjectCommand(server, scriptTarget, scriptAction);
			}
			return this.cachedObject;
		}

		private ScriptCommand GetScriptRole(Urn item, Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction)
		{
			if (item.Parent == null)
			{
				throw new InvalidOperationException();
			}
			if (item.Parent.Type == "Database")
			{
				return this.GetScriptDatabaseRole(server, scriptTarget, scriptAction);
			}
			return this.GetScriptObject(server, scriptTarget, scriptAction);
		}

		private ScriptCommand GetScriptUser(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptActions)
		{
			if (this.cachedUser == null)
			{
				this.cachedUser = new ScriptUserCommand(server, scriptTarget, scriptActions);
			}
			return this.cachedUser;
		}

		private ScriptCommand GetScriptDatabaseRole(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction)
		{
			if (this.cachedDatabaseRole == null)
			{
				this.cachedDatabaseRole = new ScriptDatabaseRoleCommand(server, scriptTarget, scriptAction);
			}
			return this.cachedDatabaseRole;
		}
	}
}
